package com.zzw.hj68;

// 算法复杂度O（nlogn）空间复杂度o（n）;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

/**
 * @Project: hw_java
 * @Description: 成绩排序
 * @Author: zzw
 */
public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        int flag = Integer.parseInt(br.readLine());

        Map<Integer, List<String>> map = new HashMap<>();

        String str;
        while ((str = br.readLine()) != null && !str.isEmpty()) {
            String[] strs = str.split(" ");
            int key = Integer.parseInt(strs[1]);
            List<String> names = map.getOrDefault(key, new ArrayList<>());
            names.add(strs[0]);
            map.put(key, names);
        }

        int[] nums = new int[map.size()];
        int index = 0;

        for (int key : map.keySet()) {
            nums[index++] = key;
        }

        Arrays.sort(nums);

        if (flag == 1) {  // 升序输出
            for (int i : nums) {
                for (String name : map.get(i)) {
                    System.out.println(name + " " + i);
                }
            }
        } else {
            for (int i = nums.length - 1; i >= 0; i--) {
                int key = nums[i];
                for (String name : map.get(key)) {
                    System.out.println(name + " " + key);
                }
            }
        }
    }
}
